﻿using System;
using System.Collections.Generic;

namespace Sinacor.Infra.Core.Licensing.Business
{
    public class Empresa
    {
        Data.Empresa _resourceAccess;
        object locker = new object();

        public Empresa()
        {
            _resourceAccess = new Data.Empresa();
        }

        public bool VerificarDependenciaLicenca(Sinacor.Infra.Core.Licensing.Company library)
        {
            return _resourceAccess.VerificarDependenciaLicenca(library);
        }

        public List<Sinacor.Infra.Core.Licensing.Company> Selecionar(Sinacor.Infra.Core.Licensing.Company library)
        {
            return _resourceAccess.Selecionar(library);
        }

        public Sinacor.Infra.Core.Licensing.Company SelecionarEmpresa(Sinacor.Infra.Core.Licensing.Company library)
        {
            return _resourceAccess.SelecionarEmpresa(library);
        }
        
        public Sinacor.Infra.Core.Licensing.Company Incluir(Sinacor.Infra.Core.Licensing.Company library)
        {
            Sinacor.Infra.Core.Licensing.Company company = _resourceAccess.SelecionarEmpresa(library);

           if (company == null)
           {
                library.CompanyId = _resourceAccess.SelecionarProximoIdEmpresa();

               _resourceAccess.Incluir(library);
               company = library;
            }
           return company;
        }

        public void Alterar(Sinacor.Infra.Core.Licensing.Company entity)
        {
            _resourceAccess.Alterar(entity);
        }

        public void Excluir(Sinacor.Infra.Core.Licensing.Company entity)
        {
            _resourceAccess.Excluir(entity);
        }

        public void ExcluirLicencaEmpresaModulo(Sinacor.Infra.Core.Licensing.LicensingCompanyModule entity)
        {
            _resourceAccess.ExcluirLicencaEmpresaModulo(entity);
        }

        int SelecionarProximoIdEmpresa()
        {
            int nextId = 0;

            lock (locker)
            {
                nextId = _resourceAccess.SelecionarProximoIdEmpresa();
            }

            return nextId;
        }

		//*----------------------------------------------------------------------------------

		public List<Sinacor.Infra.Core.Licensing.Module> RetrieveModules()
		{
			return _resourceAccess.RetrieveModules();
		}

        public bool VerificarEmpresaExiste(Sinacor.Infra.Core.Licensing.Company library)
        {
            return _resourceAccess.VerificarEmpresaExiste(library);
        }

        public Sinacor.Infra.Core.Licensing.Company SelecionarCodEmpresa(Sinacor.Infra.Core.Licensing.Company library)
        {
            return _resourceAccess.SelecionarCodEmpresa(library);
        }
    }
}